package _case

import "fmt"

type User struct {
	Name string
	Age  uint8
	Addr *Address
}

type Address struct {
	Province string
	City     string
}

func StructCase() {
	//值类型，不用初始化，会自动赋值
	u1 := User{"JC", 18, &Address{"广西", "钦州"}} //这种形式初始化，要把全部属性值都赋值
	fmt.Println(u1)                            //Addr 是一个指针地址
	fmt.Println(u1.Addr)                       //这样才能打印具体内容

	u2 := User{
		Name: "nick",
		Age:  18,
	}
	fmt.Println(u2)
	setAge(u1)
	setAge(u2)
	fmt.Println(u1, u2) //不会发生任何改变

	setAgePoter(&u1)
	setAgePoter(&u2)
	fmt.Println(u1, u2) //传递指针，值发生改变

	//指针类型
	u3 := &User{"Tom", 24, &Address{"广西", "南宁"}} //这种形式初始化，要把全部属性值都赋值
	setAgePoter(u3)
	fmt.Println(u3)

	u4 := new(User) //开辟内存控制，分配地址给变量，并不会初始化
	fmt.Println(u4) //&{ 0}  有默认值
	u4.Name = "Lily"
	u4.Age = 17
	fmt.Println(u4)

	//指针类型访问必须初始化，否则报错，空指针异常
	//u4.Addr.Province = "广东"
	//u4.Addr.City = "湛江"
	//fmt.Println(u4.Addr.Province)

	u4.Addr = &Address{"广东", "深圳"}
	fmt.Println(u4.Addr.Province)
	fmt.Println(u4.Addr.City)
}

func setAge(u User) {
	u.Age = 10
}

func setAgePoter(u *User) {
	u.Age = 10
}
